package cn.lili.modules.promotion.entity.dos;

import cn.hutool.core.util.StrUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.promotion.entity.enums.GreenAmountInfoStatusEnum;
import cn.lili.modules.promotion.entity.enums.GreenAmountInfoTypeEnum;
import cn.lili.mybatis.model.BaseStandardEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/18 14:07
 */
@Data
@TableName("li_green_contribution_info")
@ApiModel(value = "绿色积分池-贡献值详情")
public class GreenContributionInfo  extends BaseStandardEntity {

    @ApiModelProperty(value = "用户ID")
    private String userId;

    @ApiModelProperty(value = "用户类型")
    private String userType;

    @ApiModelProperty(value = "会员用户名")
    private String username;

    @ApiModelProperty(value = "会员昵称")
    private String nickName;

    @ApiModelProperty(value = "会员头像")
    private String face;

    @ApiModelProperty(value = "订单编号")
    private String orderSn;

    @ApiModelProperty(value = "类型枚举（购买商品，领取奖金，自动分红）")
    private String type;

    @ApiModelProperty(value = "贡献值数量")
    private BigDecimal umber;

    @ApiModelProperty(value = "绿色积分值")
    private BigDecimal scoreNumber;

    @ApiModelProperty(value = "状态（收入，支出，待结算，已取消）")
    private String status;

    /*******************查询字段***********/
    @TableField(exist = false)
    @ApiModelProperty(value = "类型值")
    private String typeValue;

    @TableField(exist = false)
    @ApiModelProperty(value = "状态值")
    private String statusValue;

    @TableField(exist = false)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "开始时间", hidden = true)
    private Date beginTime;

    @TableField(exist = false)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "结束时间", hidden = true)
    private Date endTime;

    @TableField(exist = false)
    private String queryType;

    @ApiModelProperty("更改后余额")
    private BigDecimal afterBalance;

    @ApiModelProperty("工会ID")
    private String unionId;


    public LambdaQueryWrapper<GreenContributionInfo> queryWrapperPage() {
        LambdaQueryWrapper<GreenContributionInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(userId),GreenContributionInfo::getUserId,userId);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(nickName),GreenContributionInfo::getNickName,nickName);
        if(StringUtils.isNotEmpty(queryType)){
            if(StringUtils.isEmpty(status)){
                List<String> list = new ArrayList<>();
                list.add(GreenAmountInfoStatusEnum.INCOME.name());
                list.add(GreenAmountInfoStatusEnum.DISBURSE.name());
                lambdaQueryWrapper.in(GreenContributionInfo::getStatus,list);
            }else{
                List<String> list = new ArrayList<>();
                list.add(status);
                list.add(GreenAmountInfoStatusEnum.CANCELLED.name());
                lambdaQueryWrapper.in(GreenContributionInfo::getStatus,list);
            }
        }else{
            lambdaQueryWrapper.eq(StringUtils.isNotEmpty(status),GreenContributionInfo::getStatus,status);
        }
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(orderSn),GreenContributionInfo::getOrderSn,orderSn);
        lambdaQueryWrapper.in(StringUtils.isNotEmpty(type),GreenContributionInfo::getType, StringUtils.isNotEmpty(type)?Arrays.asList(type.split(",")):null);
        lambdaQueryWrapper.eq(GreenContributionInfo::getDeleteFlag,false);
        lambdaQueryWrapper.ge(beginTime != null,GreenContributionInfo::getCreateTime,beginTime);
        lambdaQueryWrapper.le(endTime != null,GreenContributionInfo::getCreateTime,endTime);
        lambdaQueryWrapper.orderByDesc(GreenContributionInfo::getCreateTime);
        return lambdaQueryWrapper;
    }

    public void initGreenContributionInfo(User user){
        this.userId = user.getId();
        this.username = user.getUsername();
        this.nickName = StrUtil.isNotEmpty(user.getNickName()) ? user.getNickName() : "";
        this.face = StrUtil.isNotEmpty(user.getFace()) ? user.getFace() : "";
    }

    /**
     * 初始化待结算绿色积分
     * */
    public void initGreenContributionInfoSettled(User user,String orderSn,double greenScore){
        this.userId = user.getId();
        this.username = StrUtil.isNotEmpty(user.getUsername()) ? user.getUsername() : "";
        this.nickName = StrUtil.isNotEmpty(user.getNickName()) ? user.getNickName() : "";
        this.face = StrUtil.isNotEmpty(user.getFace()) ? user.getFace() : "";
        this.orderSn = orderSn;
        this.type = GreenAmountInfoTypeEnum.BUY_GOODS.name();
        this.scoreNumber = new BigDecimal(greenScore);
        this.status = GreenAmountInfoStatusEnum.TO_BE_SETTLED.name();
    }
}
